import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import Layout from '../components/layout/LayoutIndex.vue'

const routes: Array<RouteRecordRaw> = [
	{
		path: '/login',
		name: 'login',
		component: () => import('@/views/page/loginView.vue')
	},
	{
		path: '/',
		name: 'home',
		redirect: '/staffManage',
		component: Layout,
		children: [
			// 员工管理
			{
				path: '/staffManage',
				name: 'StaffManage',
				component: () => import('@/views/page/staffManage.vue'),
				children: []
			},

			// 员工详情
			{
				path: '/staffInfo',
				name: 'StaffInfo',
				component: () => import('@/views/page/staffInfo.vue')
			},

			// 分类管理
			{
				path: '/classifyManage',
				name: 'ClassifyManage',
				component: () => import('@/views/page/classifyManage.vue')
			},
			// 菜品管理
			{
				path: '/cuisineManage',
				name: 'CuisineManage',
				component: () => import('@/views/page/page3.vue')
			},
			// 套餐管理
			{
				path: '/comboManage',
				name: 'ComboManage',
				component: () => import('@/views/comboManage/comboManage.vue')
			},

			// 订单明细
			{
				path: '/orderDetail',
				name: 'OrderDetail',
				component: () => import('@/views/page/page3.vue')
			},

			// 菜品管理
			{
				path: '/dishesIndex',
				name: 'dishesIndex',
				component: () => import('@/views/dishes/dishesIndex.vue')
			}
		]
	},
	// 移动端首页
	{
		path: '/movingEnd',
		name: 'movingEnd',
		component: () => import('@/views/movingEnd/indexView.vue')
	},
	{
		path: '/userIndex',
		name: 'userIndex',
		component: () => import('@/views/movingEnd/userIndex.vue')
	},
	{
		path: '/addressInfo',
		name: 'addressInfo',
		component: () => import('@/views/movingEnd/addressInfo.vue')
	},
	{
		path: '/saveAddress',
		name: 'saveAddress',
		component: () => import('@/views/movingEnd/saveAddress.vue')
	},
	{
		path: '/confirmationPage',
		name: 'confirmationPage',
		component: () => import('@/views/movingEnd/confirmationPage.vue')
	}
]

const router = createRouter({
	history: createWebHashHistory(),
	routes
})

router.beforeEach((to, from, next) => {
	// next()
	const token = localStorage.getItem('token')
	if (to.path !== '/login') {
		if (token) return next()
		next('/login')
	} else {
		if (token) return next('/')
		next()
	}
})

export default router
